package com.zk.algorithm.tree;

import com.zk.algorithm.annotation.TouTiao;
import com.zk.algorithm.bean.TreeNode;

/**
 * 调整一棵二叉树，调整后，要求所有节点的右子树的最大值大于左子树的最大值
 *
 * @author zk
 */
@TouTiao("https://www.nowcoder.com/discuss/147325?type=2&order=0&pos=63&page=1")
public class BinaryTreeAdjust {

    public int maxV(TreeNode root) {
        if (root == null)
            return Integer.MIN_VALUE;

        if (maxV(root.left) > maxV(root.right)) {
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
        }

        return Math.max(root.val, Math.max(maxV(root.left), maxV(root.right)));
    }

}
